package cc.ufinity.networkdiagnose.adapter;

import java.util.List;

import cc.ufinity.networkdiagnose.manager.GroupResult;
import cc.ufinity.networkdiagnose.manager.Result;
import cc.ufinity.networkdiagnose.view.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ResultExpandableListAdapter extends BaseExpandableListAdapter {

	private Context context;
	private List<GroupResult> resultList;
	
	public ResultExpandableListAdapter(Context context, List<GroupResult> resultList) {
		this.context = context;
		this.resultList = resultList;
	}
	
	public Object getChild(int groupPosition, int childPosition) {
		return resultList.get(groupPosition).getResultList().get(childPosition);
	}

	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
        
        Result result = resultList.get(groupPosition).getResultList().get(childPosition);
        
		View view = convertView;
		
        if(view == null){
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.child, null);
        }
        
        final TextView name = (TextView) view.findViewById(R.id.witchTaskFinishLayoutTitle);
        
        name.setText(result.getName());

        final TextView info = (TextView) view.findViewById(R.id.witchTaskFinishLayout);
        info.setText(result.getInfo());

		return view;
	}

	public int getChildrenCount(int groupPosition) {
        return resultList.get(groupPosition).getResultList().size();
	}

	public Object getGroup(int groupPosition) {
		return resultList.get(groupPosition);
		//return groupPosition;
	}

	public int getGroupCount() {
		return resultList.size();
	}

	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		View groupView = convertView;

		LayoutInflater inflater_new = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		groupView = inflater_new.inflate( R.layout.group , null);
		
		String title = resultList.get(groupPosition).getName();
        TextView groupName = (TextView) groupView.findViewById(R.id.group_name);
        groupName.setText(title);
        
        ImageView groupImg = (ImageView) groupView.findViewById(R.id.group_img);
        if(isExpanded) {
        	groupImg.setImageResource(R.drawable.icon_open);
        } else {
        	groupImg.setImageResource(R.drawable.icon_close);
        }
        
        return groupView;
	}

	public boolean hasStableIds() {
		return false;
	}

	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}
	
}
